package cn.edu.jsu.zjj.controller;

import cn.edu.jsu.zjj.annotation.Type;
import cn.edu.jsu.zjj.common.result.PageResult;
import cn.edu.jsu.zjj.common.result.Result;
import cn.edu.jsu.zjj.constant.MessageConstant;
import cn.edu.jsu.zjj.exception.BaseException;
import cn.edu.jsu.zjj.exception.ThrowUtils;
import cn.edu.jsu.zjj.model.dto.filter.DocuFilterRequest;
import cn.edu.jsu.zjj.model.entity.DocuFilter;
import cn.edu.jsu.zjj.service.DocuFilterService;
import cn.edu.jsu.zjj.service.UserService;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/docu-filter")
@Api(tags = "筛选相关接口")
@Slf4j
public class DocuFilterController {
    @Autowired
    private DocuFilterService docuFilterService;
    @Autowired
    private UserService userService;

    /**
     * 新增筛选
     *
     * @param docuFilterRequest 资料筛选内容
     * @return
     */
    @Type("新增筛选")
    @ApiOperation("新增筛选")
    @PostMapping
    public Result save(@RequestBody DocuFilterRequest docuFilterRequest) {
        if(docuFilterRequest.getData() == null){
            throw new BaseException(MessageConstant.DATA_NULL);
        }
        if (docuFilterRequest.getDocuId() <= 0) {
            throw new BaseException(MessageConstant.PARAMS_ERROR);
        }
        if (StringUtils.isEmpty(docuFilterRequest.getHeaderKey()) || docuFilterRequest.getHeaderId() <= 0) {
            throw new BaseException(MessageConstant.PARAMS_ERROR);
        }
        LambdaQueryWrapper<DocuFilter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DocuFilter::getDocuId, docuFilterRequest.getDocuId());
        queryWrapper.eq(DocuFilter::getData, docuFilterRequest.getData());
        queryWrapper.eq(DocuFilter::getIsDeleted, 0);
        if (docuFilterService.getOne(queryWrapper) != null) {
            return Result.error("筛选已存在");
        }

        DocuFilter docuFilter = new DocuFilter();
        BeanUtils.copyProperties(docuFilterRequest, docuFilter);
        docuFilter.setUserId(userService.getLoginUser().getId());
        docuFilter.setUserName(userService.getLoginUser().getName());
        docuFilter.setCreateTime(new Date());
        docuFilter.setUpdateTime(new Date());


        boolean result = docuFilterService.saveOrUpdate(docuFilter);
        ThrowUtils.throwIf(!result, MessageConstant.OPERATION_ERROR);
        return Result.success();
    }

    /**
     * 删除某个表头
     *
     * @param id 筛选id
     * @return
     */
    @ApiOperation("删除某个筛选条件")
    @GetMapping("/delete/{id}")
    public Result<DocuFilter> deleteHead(@PathVariable Long id) {
        if (id <= 0) {
            throw new BaseException(MessageConstant.PARAMS_ERROR);
        }
        LambdaUpdateWrapper<DocuFilter> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DocuFilter::getId, id);
        updateWrapper.set(DocuFilter::getIsDeleted, 1);
        boolean update = docuFilterService.update(null, updateWrapper);
        if (!update) {
            return Result.error("删除失败");
        }
        return Result.success();
    }


    /**
     * 根据id查询筛选
     *
     * @param docuId
     * @return
     */
    @ApiOperation("根据资料id查询")
    @PostMapping("/{docuId}")
    public Result<PageResult> getById(@PathVariable Long docuId, @RequestBody DocuFilterRequest docuFilterRequest) {
        if (docuId <= 0) {
            throw new BaseException(MessageConstant.PARAMS_ERROR);
        }
        long size = docuFilterRequest.getPageSize();
        int current = docuFilterRequest.getCurrent();

        LambdaQueryWrapper<DocuFilter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DocuFilter::getDocuId, docuId);
        queryWrapper.eq(DocuFilter::getIsDeleted, 0);

        Page<DocuFilter> result = docuFilterService.page(new Page<>(current, size), queryWrapper);
        return Result.success(new PageResult(result.getTotal(), result.getRecords()));
    }

    /**
     * 根据id查询筛选
     *
     * @param docuId
     * @return
     */
    @ApiOperation("根据资料id查询")
    @GetMapping("/{docuId}")
    public Result<List<DocuFilter>> getById(@PathVariable Long docuId) {
        if (docuId <= 0) {
            throw new BaseException(MessageConstant.PARAMS_ERROR);
        }

        LambdaQueryWrapper<DocuFilter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DocuFilter::getDocuId, docuId);
        queryWrapper.eq(DocuFilter::getIsDeleted, 0);

        List<DocuFilter> result = docuFilterService.list(queryWrapper);
        return Result.success(result);
    }
}
